<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="所在区域id" prop="parkAreaId">
      <el-input v-model="dataForm.parkAreaId" placeholder="所在区域id"></el-input>
    </el-form-item>
    <el-form-item label="管理员id" prop="parkAdminId">
      <el-input v-model="dataForm.parkAdminId" placeholder="管理员id"></el-input>
    </el-form-item>
    <el-form-item label="收费标准id" prop="parkSettleId">
      <el-input v-model="dataForm.parkSettleId" placeholder="收费标准id"></el-input>
    </el-form-item>
    <el-form-item label="停车场名称" prop="parkName">
      <el-input v-model="dataForm.parkName" placeholder="停车场名称"></el-input>
    </el-form-item>
    <el-form-item label="详细地址" prop="parkAddress">
      <el-input v-model="dataForm.parkAddress" placeholder="详细地址"></el-input>
    </el-form-item>
    <el-form-item label="停车场展示图" prop="parkPic">
      <el-input v-model="dataForm.parkPic" placeholder="停车场展示图"></el-input>
    </el-form-item>
    <el-form-item label="停车场总车位数量" prop="parkTotle">
      <el-input v-model="dataForm.parkTotle" placeholder="停车场总车位数量"></el-input>
    </el-form-item>
    <el-form-item label="已用车位数量" prop="parkInUse">
      <el-input v-model="dataForm.parkInUse" placeholder="已用车位数量"></el-input>
    </el-form-item>
    <el-form-item label="坐标经度" prop="parkLatitude">
      <el-input v-model="dataForm.parkLatitude" placeholder="坐标经度"></el-input>
    </el-form-item>
    <el-form-item label="坐标纬度" prop="parkLongitude">
      <el-input v-model="dataForm.parkLongitude" placeholder="坐标纬度"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          parkId: 0,
          parkAreaId: '',
          parkAdminId: '',
          parkSettleId: '',
          parkName: '',
          parkAddress: '',
          parkPic: '',
          parkTotle: '',
          parkInUse: '',
          parkLatitude: '',
          parkLongitude: '',
          addTime: '',
          updateTime: '',
          daleted: ''
        },
        dataRule: {
          parkAreaId: [
            { required: true, message: '所在区域id不能为空', trigger: 'blur' }
          ],
          parkAdminId: [
            { required: true, message: '管理员id不能为空', trigger: 'blur' }
          ],
          parkSettleId: [
            { required: true, message: '收费标准id不能为空', trigger: 'blur' }
          ],
          parkName: [
            { required: true, message: '停车场名称不能为空', trigger: 'blur' }
          ],
          parkAddress: [
            { required: true, message: '详细地址不能为空', trigger: 'blur' }
          ],
          parkPic: [
            { required: true, message: '停车场展示图不能为空', trigger: 'blur' }
          ],
          parkTotle: [
            { required: true, message: '停车场总车位数量不能为空', trigger: 'blur' }
          ],
          parkInUse: [
            { required: true, message: '已用车位数量不能为空', trigger: 'blur' }
          ],
          parkLatitude: [
            { required: true, message: '坐标经度不能为空', trigger: 'blur' }
          ],
          parkLongitude: [
            { required: true, message: '坐标纬度不能为空', trigger: 'blur' }
          ],
          addTime: [
            { required: true, message: '添加时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          daleted: [
            { required: true, message: '逻辑删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.parkId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.parkId) {
			this.$axios.get(`/park/info/${this.dataForm.parkId}`).then((data)=>{
				if (data && data.data.code === 0) {
					this.dataForm.parkAreaId = data.data.park.parkAreaId
					this.dataForm.parkAdminId = data.data.park.parkAdminId
					this.dataForm.parkSettleId = data.data.park.parkSettleId
					this.dataForm.parkName = data.data.park.parkName
					this.dataForm.parkAddress = data.data.park.parkAddress
					this.dataForm.parkPic = data.data.park.parkPic
					this.dataForm.parkTotle = data.data.park.parkTotle
					this.dataForm.parkInUse = data.data.park.parkInUse
					this.dataForm.parkLatitude = data.data.park.parkLatitude
					this.dataForm.parkLongitude = data.data.park.parkLongitude
				}
			}).catch((error)=>{
				console.log(error)
			})
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
			var params = {
				'parkId': this.dataForm.parkId || undefined,
				'parkAreaId': this.dataForm.parkAreaId,
				'parkAdminId': this.dataForm.parkAdminId,
				'parkSettleId': this.dataForm.parkSettleId,
				'parkName': this.dataForm.parkName,
				'parkAddress': this.dataForm.parkAddress,
				'parkPic': this.dataForm.parkPic,
				'parkTotle': this.dataForm.parkTotle,
				'parkInUse': this.dataForm.parkInUse,
				'parkLatitude': this.dataForm.parkLatitude,
				'parkLongitude': this.dataForm.parkLongitude
			}
			this.$axios.post(`/park/${!this.dataForm.parkId ? 'save' : 'update'}`, params).then((data)=>{
				if (data && data.data.code === 0) {
				  this.$message({
				    message: '操作成功',
				    type: 'success',
				    duration: 1500,
				    onClose: () => {
				      this.visible = false
				      this.$emit('refreshDataList')
				    }
				  })
				} else {
				  this.$message.error(data.data.msg)
				}
			}).catch((error)=>{
				console.log(error)
			})
          }
        })
      }
    }
  }
</script>
